package com.bidevalution.business.job;

import com.bidevalution.business.factory.StepJobJdbcFactory;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.BeSectionStepJob;
import com.bidevalution.support.context.Constants;
import com.bidevalution.support.exception.BizException;
import com.google.common.collect.Maps;
import com.qtp.core.util.OConvertUtils;
import org.apache.log4j.Logger;

import java.util.Map;

/**
 * 简易评标-合理低价
 * @author huyiqi
 * @date 2019/12/11
 */
public class LowPriceEntNumCheckJob implements StepJobJdbcFactory {

    private static Logger log = Logger.getLogger(LowPriceEntNumCheckJob.class);
    private JdbcTemplateService jdbcTemplateService;

    @Override
    public Boolean execute(BeSectionStepJob beSectionStepJob, String submitCode, String expertId, String layoutCode) throws Exception {
        try {
            Map<String, Object> paramMap = Maps.newHashMap();
            paramMap.put("sectionId", beSectionStepJob.getSectionId());
            paramMap.put("sectionStepId", beSectionStepJob.getSectionStepId());
            paramMap.put("submitCode", submitCode);
            paramMap.put("isDel", "0");
            paramMap.put("expertId",expertId);
            String sqlstr = "SELECT is_manager FROM be_section_expert_info WHERE seq_id=:expertId";
            //只有组长才做判断，因为这个环节的结果是组长决定
            if(Constants.STR_YES.equals(OConvertUtils.getString(jdbcTemplateService.selectForMap(sqlstr, paramMap).get("is_manager")))){
                String sql = "select count(*) as num from be_evalution_outcome where "
                        + "section_step_id=:sectionStepId and section_id=:sectionId and submit_code=:submitCode " +
                        "and is_del=:isDel and expert_id=:expertId and c1 = 1";
                Map<String, Object> map = jdbcTemplateService.selectForMap(sql, paramMap);
                if(map != null && !map.isEmpty()) {
                    int num = OConvertUtils.getInt(map.get("num"), 0);
                    if(num > 1) {
                        throw new BizException("只有一家单位可以通过该阶段。");
                    }
                }
            }
        } catch (Exception e) {
            log.error("简易法-合理低价审核错误："+e.getMessage(), e);
            throw e;
        }
        return false;
    }

    @Override
    public void setJdbcTemplateService(JdbcTemplateService jdbcTemplateService) {
        this.jdbcTemplateService = jdbcTemplateService;
    }
}
